package com.ruoyi.eval.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.eval.domain.EvalQuestionOption;
import com.ruoyi.eval.mapper.EvalQuestionMapper;
import com.ruoyi.eval.domain.EvalQuestion;
import com.ruoyi.eval.service.IEvalQuestionService;

/**
 * 评价题目Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-05-06
 */
@Service
public class EvalQuestionServiceImpl implements IEvalQuestionService 
{
    private static final Logger log = LoggerFactory.getLogger(EvalQuestionServiceImpl.class);

    @Autowired
    private EvalQuestionMapper evalQuestionMapper;

    /**
     * 查询评价题目
     * 
     * @param id 评价题目主键
     * @return 评价题目
     */
    @Override
    public EvalQuestion selectEvalQuestionById(Long id)
    {
        return evalQuestionMapper.selectEvalQuestionById(id);
    }

    /**
     * 查询评价题目列表
     * 
     * @param evalQuestion 评价题目
     * @return 评价题目
     */
    @Override
    public List<EvalQuestion> selectEvalQuestionList(EvalQuestion evalQuestion)
    {
        return evalQuestionMapper.selectEvalQuestionList(evalQuestion);
    }

    /**
     * 新增评价题目
     * 
     * @param evalQuestion 评价题目
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEvalQuestion(EvalQuestion evalQuestion)
    {
        evalQuestion.setCreateTime(DateUtils.getNowDate());
        int rows = evalQuestionMapper.insertEvalQuestion(evalQuestion);
        if(!evalQuestion.getType().equals(EvalQuestion.TYPE_QA)) {
            insertEvalQuestionOption(evalQuestion);
        }
        return rows;
    }

    /**
     * 修改评价题目
     * 
     * @param evalQuestion 评价题目
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEvalQuestion(EvalQuestion evalQuestion)
    {
        evalQuestion.setUpdateTime(DateUtils.getNowDate());
        evalQuestionMapper.deleteEvalQuestionOptionByQuestionId(evalQuestion.getId());
        if(!evalQuestion.getType().equals(EvalQuestion.TYPE_QA)) {
            insertEvalQuestionOption(evalQuestion);
        }
        return evalQuestionMapper.updateEvalQuestion(evalQuestion);
    }

    /**
     * 批量删除评价题目
     * 
     * @param ids 需要删除的评价题目主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEvalQuestionByIds(Long[] ids)
    {
        evalQuestionMapper.deleteEvalQuestionOptionByQuestionIds(ids);
        return evalQuestionMapper.deleteEvalQuestionByIds(ids);
    }

    /**
     * 删除评价题目信息
     * 
     * @param id 评价题目主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEvalQuestionById(Long id)
    {
        evalQuestionMapper.deleteEvalQuestionOptionByQuestionId(id);
        return evalQuestionMapper.deleteEvalQuestionById(id);
    }

    /**
     * 新增题目选项信息
     * 
     * @param evalQuestion 评价题目对象
     */
    public void insertEvalQuestionOption(EvalQuestion evalQuestion)
    {
        List<EvalQuestionOption> evalQuestionOptionList = evalQuestion.getEvalQuestionOptionList();
        Long id = evalQuestion.getId();
        if (StringUtils.isNotNull(evalQuestionOptionList))
        {
            List<EvalQuestionOption> list = new ArrayList<EvalQuestionOption>();
            for (EvalQuestionOption evalQuestionOption : evalQuestionOptionList)
            {
                evalQuestionOption.setQuestionId(id);
                list.add(evalQuestionOption);
            }
            if (list.size() > 0)
            {
                evalQuestionMapper.batchEvalQuestionOption(list);
            }
        }
    }


}
